<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>MyBatis学习 | 牛牛&amp;blog</title><meta name="author" content="牛牛"><meta name="copyright" content="牛牛"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="MyBatis 框架第一章 框架的概述1.三层架构mvc：web开发中，使用mvc架构模式。  m：数据， v：视图， c：控制器。 ​          c控制器： 接收请求，调用service对象，显示请求的处理结果。 当前使用servlet作为控制器 ​          v视图： 现在使用jsp， html，css，js。 显示请求的处理结果，把m中数据显示出来。 ​          m">
<meta property="og:type" content="article">
<meta property="og:title" content="MyBatis学习">
<meta property="og:url" content="https://code-ran.gitee.io/2022/11/23/mybatis%E5%AD%A6%E4%B9%A0/index.html">
<meta property="og:site_name" content="牛牛&amp;blog">
<meta property="og:description" content="MyBatis 框架第一章 框架的概述1.三层架构mvc：web开发中，使用mvc架构模式。  m：数据， v：视图， c：控制器。 ​          c控制器： 接收请求，调用service对象，显示请求的处理结果。 当前使用servlet作为控制器 ​          v视图： 现在使用jsp， html，css，js。 显示请求的处理结果，把m中数据显示出来。 ​          m">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="">
<meta property="article:published_time" content="2022-11-23T07:28:14.000Z">
<meta property="article:modified_time" content="2023-11-02T03:32:07.190Z">
<meta property="article:author" content="牛牛">
<meta property="article:tag" content="MyBatis">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content=""><link rel="shortcut icon" href="/img/myfavicon.png"><link rel="canonical" href="https://code-ran.gitee.io/2022/11/23/mybatis%E5%AD%A6%E4%B9%A0/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'MyBatis学习',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-11-02 11:32:07'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/style.css"><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>const preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',()=> { preloader.endLoading() })

if (false) {
  document.addEventListener('pjax:send', () => { preloader.initLoading() })
  document.addEventListener('pjax:complete', () => { preloader.endLoading() })
}</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/ys.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">52</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">26</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">26</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">牛牛&amp;blog</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">MyBatis学习</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-11-23T07:28:14.000Z" title="发表于 2022-11-23 15:28:14">2022-11-23</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-11-02T03:32:07.190Z" title="更新于 2023-11-02 11:32:07">2023-11-02</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/MyBatis%E7%9B%B8%E5%85%B3/">MyBatis相关</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="MyBatis学习"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="MyBatis-框架"><a href="#MyBatis-框架" class="headerlink" title="MyBatis 框架"></a>MyBatis 框架</h1><h1 id="第一章-框架的概述"><a href="#第一章-框架的概述" class="headerlink" title="第一章 框架的概述"></a>第一章 框架的概述</h1><h2 id="1-三层架构"><a href="#1-三层架构" class="headerlink" title="1.三层架构"></a>1.三层架构</h2><p>mvc：web开发中，使用mvc架构模式。  m：数据， v：视图， c：控制器。</p>
<p>​          c控制器： 接收请求，调用service对象，显示请求的处理结果。 当前使用servlet作为控制器</p>
<p>​          v视图： 现在使用jsp， html，css，js。 显示请求的处理结果，把m中数据显示出来。</p>
<p>​          m数据： 来自数据库mysql， 来自文件，来自网络</p>
<p>mvc作用：</p>
<p>​     1）实现解耦合。 </p>
<p>​     2）让mvc 各负其职。  </p>
<p>​     3）使的系统扩展更好。更容易维护。 </p>
<p>三层架构：</p>
<p>1.界面层（视图层）：接收用户的请求，调用service， 显示请求的处理结果的。 包含了jsp，html，servlet等对象。 对应的包controller，</p>
<p>2.业务逻辑层：处理业务逻辑， 使用算法处理数据的。 把数据返回给界面层。 对应的是service包，和包中的很多的XXXService类。 例如：  StudentService ， OrderService, ShopService </p>
<p>3.持久层（数据库访问层）：访问数据库，或者读取文件，访问网络。获取数据。 对应的包是dao。 dao包中很多的StudentDao,  OrderDao,  ShopDao等等。</p>
<h2 id="2-三层架构请求的处理流程"><a href="#2-三层架构请求的处理流程" class="headerlink" title="2. 三层架构请求的处理流程"></a>2. 三层架构请求的处理流程</h2><p>用户发起请求—-&gt;界面层—–&gt;业务逻辑层—-&gt;持久层—-&gt;数据库（mysql）</p>
<h2 id="3-为什么要使用三层？"><a href="#3-为什么要使用三层？" class="headerlink" title="3. 为什么要使用三层？"></a>3. 为什么要使用三层？</h2><p>1，结构清晰、耦合度低, 各层分工明确<br>2，可维护性高，可扩展性高<br>3，有利于标准化<br>4，开发人员可以只关注整个结构中的其中某一层的功能实现<br>5，有利于各层逻辑的复用</p>
<h2 id="4-三层架构模式和框架"><a href="#4-三层架构模式和框架" class="headerlink" title="4. 三层架构模式和框架"></a>4. 三层架构模式和框架</h2><p>每一层对应着一个框架</p>
<p>1）界面层—SpringMVC框架</p>
<p>2）业务层—Spring框架</p>
<p>3）持久层—MyBatis框架</p>
<h2 id="5-框架"><a href="#5-框架" class="headerlink" title="5 .框架"></a>5 .框架</h2><ol>
<li>什么是框架（framework）</li>
</ol>
<p>框架：就是一个软件， 完成了部分的功能。  软件中的类和类之间的方法调用都已经规定好了。 通过这些可以完成某些功能。  框架看做是模版。   </p>
<p>框架是可以升级的，改造的。 框架是安全的。</p>
<p>框架是对某一个方面有用的，不是全能的。 </p>
<h2 id="6-框架解决的问题"><a href="#6-框架解决的问题" class="headerlink" title="6. 框架解决的问题"></a>6. 框架解决的问题</h2><p>1）框架能实现技术的整合。 </p>
<p>2）提高开发的效率。 降低难度。</p>
<h2 id="7-jdbc访问数据库的优缺点"><a href="#7-jdbc访问数据库的优缺点" class="headerlink" title="7. jdbc访问数据库的优缺点"></a>7. jdbc访问数据库的优缺点</h2><p>优点：</p>
<ol>
<li>直观，好理解</li>
</ol>
<p>缺点：</p>
<ol>
<li>创建很多对象 Connection ,Statement, ResultSet</li>
<li>注册驱动</li>
<li>执行sql语句</li>
<li>把ResultSet转为 Student ， List集合。</li>
<li>关闭资源</li>
<li>sql语句和业务逻辑代码混在一起</li>
</ol>
<h2 id="8-MyBatis框架"><a href="#8-MyBatis框架" class="headerlink" title="8 MyBatis框架"></a>8 MyBatis框架</h2><p>什么 mybatis： 是一个持久层框架， 原名是ibatis， 2013改名为 MyBatis.   MyBatis可以操作数据库，对数据执行增删改查。  看做是高级的jdbc。  解决jdbc的缺点。</p>
<p>mybatis能做什么？</p>
<p> 1） 注册驱动 。</p>
<p>  2） 创建jdbc中使用的Connection， Statement，ResultSet</p>
<pre><code>3)   执行sql语句， 得到ResultSet

4)   处理ResultSet， 把记录集中的数据转为java对象，  同时还能把java对象放入到List集合。
</code></pre>
<p>  5）关闭资源</p>
<p>  6）实现sql语句和java代码的解耦合。</p>
<p>mybatis的文档： <a target="_blank" rel="noopener" href="https://mybatis.org/mybatis-3/zh/index.html">https://mybatis.org/mybatis-3/zh/index.html</a></p>
<h1 id="第二章-MyBatis入门"><a href="#第二章-MyBatis入门" class="headerlink" title="第二章  MyBatis入门"></a>第二章  MyBatis入门</h1><h2 id="2-1-第一个例子"><a href="#2-1-第一个例子" class="headerlink" title="2.1 第一个例子"></a>2.1 第一个例子</h2><p>实现步骤：</p>
<p>0.创建student表（id，name，email，age）</p>
<p>1.新建maven项目</p>
<p>2.修改pom.xml </p>
<p>   1）加入依赖 mybatis依赖， mysql驱动， junit</p>
<p>   2）在<build>加入资源插件</build></p>
<p>3.创建实体类Student。定义属性， 属性名和列名保持一致</p>
<p>4.创建Dao接口， 定义操作数据库的方法。</p>
<p>5.创建xml文件（mapper文件）， 写sql语句。 </p>
<p>   mybatis框架推荐是把sql语句和java代码分开</p>
<p>   mapper文件：定义和dao接口在同一目录， 一个表一个mapper文件。</p>
<p>6.创建mybatis的主配置文件（xml文件）：有一个， 放在resources目录下</p>
<p>   1）定义创建连接实例的数据源（DataSource）对象</p>
<pre><code>  2)   指定其他mapper文件的位置
</code></pre>
<p>7.创建测试的内容。</p>
<p>   使用main方法，测试mybatis访问数据库</p>
<p>   也可以使用junit 访问数据库</p>
<h2 id="2-2-概念"><a href="#2-2-概念" class="headerlink" title="2.2 概念"></a>2.2 概念</h2><p>1.自动提交：当你的 sql语句执行完毕后， 提交事务。 数据库更新操作之间保存到数据</p>
<p>2.手动（手工）提交事务：在你需要提交事务的位置， 执行方法，提交事务或者回顾事务。</p>
<h2 id="2-3-MyBatis的一些重要对象"><a href="#2-3-MyBatis的一些重要对象" class="headerlink" title="2.3 MyBatis的一些重要对象"></a>2.3 MyBatis的一些重要对象</h2><p>1） Resources ： mybatis框架中的对象， 一个作用读取 主配置信息。</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">InputStream inputStream = Resources.getResourceAsStream(<span class="string">&quot;mybatis.xml&quot;</span>);</span><br></pre></td></tr></table></figure>



<p>2）SqlSessionFactoryBuilder：负责创建SqlSessionFactory对象</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">SqlSessionFactory factory = <span class="keyword">new</span> SqlSessionFactoryBuilder().build(inputStream);</span><br></pre></td></tr></table></figure>



<p>3）SqlSessionFactory: 重要对象</p>
<p>SqlSessionFactory是重量级对象：创建此对象需要使用更多的资源和时间。  在项目中有一个就可以了。</p>
<p>SqlSessionFactory接口：作用是SqlSession的工厂， 就是创建SqlSession对象。</p>
<p>DefaultSqlSessionFactory实现类</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DefaultSqlSessionFactory</span> <span class="keyword">implements</span> <span class="title">SqlSessionFactory</span> </span>&#123; &#125; </span><br></pre></td></tr></table></figure>



<p>SqlSessionFactory接口中的方法</p>
<p>openSession(): 获取一个默认的SqlSession对象， 默认是需要手工提交事务的。</p>
<p>openSession(boolean): boolean参数表示是否自动提交事务。 </p>
<p>​                   true： 创建一个自动提交事务的SqlSession</p>
<p>​                  false:   等同于没有参数的openSession</p>
<ol start="4">
<li>SqlSession对象</li>
</ol>
<p>SqlSession对象是通过SqlSessionFactory获取的。 SqlSession本身是接口</p>
<p>DefaultSqlSession: 实现类</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DefaultSqlSession</span> <span class="keyword">implements</span> <span class="title">SqlSession</span> </span>&#123; &#125;</span><br></pre></td></tr></table></figure>

<p>SqlSession作用是提供了大量的执行sql语句的方法：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">selectOne：执行sql语句，最多得到一行记录，多余1行是错误。</span><br><span class="line">selectList：执行sql语句，返回多行数据</span><br><span class="line">selectMap：执行sql语句的，得到一个Map结果</span><br><span class="line">insert：执行insert语句</span><br><span class="line">update：执行update语句</span><br><span class="line">delete：执行delete语句</span><br><span class="line">commit：提交事务</span><br><span class="line">rollback：回顾事务</span><br></pre></td></tr></table></figure>



<p>注意SqlSession对象不是线程安全的， 使用的步骤：</p>
<p>①：在方法的内部，执行sql语句之前，先获取SqlSession对象</p>
<p>②：调用SqlSession的方法，执行sql语句</p>
<p>③：关闭SqlSession对象，执行SqlSession.close()</p>
<h2 id="2-4-使用工具类和模版"><a href="#2-4-使用工具类和模版" class="headerlink" title="2.4 使用工具类和模版"></a>2.4 使用工具类和模版</h2><p>1）创建模版，mapper文件模版和mybatis主配置文件模版</p>
<p>创建模版的步骤：</p>
<p><img src="/2022/11/23/mybatis%E5%AD%A6%E4%B9%A0/image-20201019094212830.png" alt="image-20201019094212830">    </p>
<p>创建模版文件：</p>
<p><img src="/2022/11/23/mybatis%E5%AD%A6%E4%B9%A0/image-20201019094359625.png" alt="image-20201019094359625">    </p>
<p>创建文件选择使用的模版：</p>
<p><img src="/2022/11/23/mybatis%E5%AD%A6%E4%B9%A0/image-20201019094616208.png" alt="image-20201019094616208">    </p>
<h1 id="第三章-MyBatis的Dao代理"><a href="#第三章-MyBatis的Dao代理" class="headerlink" title="第三章  MyBatis的Dao代理"></a>第三章  MyBatis的Dao代理</h1><h2 id="3-1-dao代理"><a href="#3-1-dao代理" class="headerlink" title="3.1  dao代理"></a>3.1  dao代理</h2><h3 id="3-1-1-mybatis提供代理："><a href="#3-1-1-mybatis提供代理：" class="headerlink" title="3.1.1 mybatis提供代理："></a>3.1.1 mybatis提供代理：</h3><p> mybatis创建Dao接口的实现类对象， 完成对sql语句的执行。  mybatis创建一个对象代替你的 dao实现类功能。</p>
<h3 id="3-1-2-使用mybatis代理要求"><a href="#3-1-2-使用mybatis代理要求" class="headerlink" title="3.1.2 使用mybatis代理要求"></a>3.1.2 使用mybatis代理要求</h3><p>1）mapper文件中的namespace 一定dao接口的全限定名称</p>
<p>2）mapper文件中 标签的id是dao接口方法名称</p>
<h3 id="3-1-3-mybatis代理实现方式"><a href="#3-1-3-mybatis代理实现方式" class="headerlink" title="3.1.3 mybatis代理实现方式"></a>3.1.3 mybatis代理实现方式</h3><p>使用SqlSession对象的方法 getMapper(dao.class)</p>
<p>例如： 现在有 StudentDao接口。 </p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">SqlSession session  = MyBatisUtils.getSqlSession();</span><br><span class="line">StudentDao dao  = session.getMapper(StudentDao.class);</span><br><span class="line">Student student = dao.selectById(<span class="number">1001</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//上面代码中</span></span><br><span class="line">StudentDao dao  = session.getMapper(StudentDao.class);</span><br><span class="line">等同于</span><br><span class="line">StudentDao dao  = <span class="keyword">new</span> StudentDaoImpl();</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="3-2-理解参数"><a href="#3-2-理解参数" class="headerlink" title="3.2 理解参数"></a>3.2 理解参数</h2><p>理解参数是： 通过java程序把数据传入到mapper文件中的sql语句。 参数主要是指dao接口方法的形参</p>
<h3 id="3-2-1-parameterType"><a href="#3-2-1-parameterType" class="headerlink" title="3.2.1 parameterType"></a>3.2.1 parameterType</h3><p>parameterType:表示参数的类型， 指定dao方法的形参数据类型。 这个形参的数据类型是给mybatis使用。 mybatis在给sql语句的参数赋值时使用。 PreparedStatement.setXXX( 位置， 值) </p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line">第一个用法： java类型的全限定类型名称   parameterType=&quot;java.lang.Integer&quot;</span><br><span class="line">第二个用法： mybatis定义的java类型的别名  parameterType=&quot;int&quot;</span><br><span class="line"></span><br><span class="line">parameterType：mybatis通过反射机制可以获取 dao接口方法参数的类型， 可以不写</span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectById&quot;</span>  <span class="attr">parameterType</span>=<span class="string">&quot;integer&quot;</span></span></span><br><span class="line"><span class="tag">                 <span class="attr">resultType</span>=<span class="string">&quot;com.bjpowernode.domain.Student&quot;</span>&gt;</span></span><br><span class="line">   select id,name,email,age from student where id=#&#123;studentId&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="3-2-2-dao接口方法是一个简单类型的参数"><a href="#3-2-2-dao接口方法是一个简单类型的参数" class="headerlink" title="3.2.2 dao接口方法是一个简单类型的参数"></a>3.2.2 dao接口方法是一个简单类型的参数</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//dao接口的方法形参是一个简单类型的</span></span><br><span class="line"><span class="comment">//简单类型： java基本数据类型和String</span></span><br><span class="line"><span class="function">Student <span class="title">selectByEmail</span><span class="params">(String email)</span></span>;</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">   dao接口是一个简单类型的参数</span></span><br><span class="line"><span class="comment">   mapper文件，获取这个参数值，使用#&#123;任意字符&#125;</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectByEmail&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;com.bjpowernode.domain.Student&quot;</span>&gt;</span></span><br><span class="line">    select id,name,email,age from student where email=#&#123;studentEmail&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h3 id="3-2-3-dao接口方法有多个简单类型的参数"><a href="#3-2-3-dao接口方法有多个简单类型的参数" class="headerlink" title="3.2.3 dao接口方法有多个简单类型的参数"></a>3.2.3 dao接口方法有多个简单类型的参数</h3><p>@Param: 命名参数， 在方法的形参前面使用的， 定义参数名。  这个名称可以用在mapper文件中。</p>
<p>dao接口，方法的定义</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">  多个简单类型的参数</span></span><br><span class="line"><span class="comment">  使用@Param命名参数， 注解是mybatis提供的</span></span><br><span class="line"><span class="comment">  位置：在形参定义的前面</span></span><br><span class="line"><span class="comment">  属性：value 自定义的参数名称</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function">List&lt;Student&gt; <span class="title">selectByNameOrAge</span><span class="params">(<span class="meta">@Param(&quot;myname&quot;)</span> String name,</span></span></span><br><span class="line"><span class="params"><span class="function">                                <span class="meta">@Param(&quot;myage&quot;)</span> Integer age)</span></span>;</span><br></pre></td></tr></table></figure>



<p>mapper文件</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">   多个简单类型的参数.</span></span><br><span class="line"><span class="comment">   当使用了@Param命名后，例如@Param(&quot;myname&quot;).</span></span><br><span class="line"><span class="comment">   在mapper中，使用#&#123;命名的参数&#125;， 例如 #&#123;myname&#125;</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectByNameOrAge&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;com.bjpowernode.domain.Student&quot;</span>&gt;</span></span><br><span class="line">    select id,name,email,age from student where name=#&#123;myname&#125; or age=#&#123;myage&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h3 id="3-2-4-dao接口方法使用一个对象作为参数"><a href="#3-2-4-dao接口方法使用一个对象作为参数" class="headerlink" title="3.2.4 dao接口方法使用一个对象作为参数"></a>3.2.4 dao接口方法使用一个对象作为参数</h3><p>方法的形参是一个java对象。这个java对象表示多个参数。使用对象的属性值作为参数使用</p>
<p>java对象</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Student</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> Integer id;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> String email;</span><br><span class="line">    <span class="keyword">private</span> Integer age;</span><br><span class="line">    <span class="comment">//set|get方法</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">QueryParam</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> Object p1;</span><br><span class="line">    <span class="keyword">private</span> Object p2;</span><br><span class="line">    <span class="comment">//set|get方法</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<p>dao接口中的方法定义</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * 一个java对象作为参数( 对象由属性， 每个属性有set，get方法)</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function">List&lt;Student&gt; <span class="title">selectByObject</span><span class="params">(Student student)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function">List&lt;Student&gt; <span class="title">selectByQueryParam</span><span class="params">(QueryParam param)</span></span>;</span><br></pre></td></tr></table></figure>



<p>mapper文件</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">   一个java对象作为方法的参数，使用对象的属性作为参数值使用</span></span><br><span class="line"><span class="comment">   简单的语法： #&#123;属性名&#125; ， mybatis调用此属性的getXXX()方法获取属性值</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectByObject&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;com.bjpowernode.domain.Student&quot;</span>&gt;</span></span><br><span class="line">    select id,name,email,age from student where name=#&#123;name&#125; or age=#&#123;age&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectByQueryParam&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;com.bjpowernode.domain.Student&quot;</span>&gt;</span></span><br><span class="line">     select id,name,email,age from student where name=#&#123;p1&#125; or age=#&#123;p2&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!--负责的语法格式： #&#123;属性名,javaType=java类型的全限定名称,jdbcType=mybatis中定义列的数据类型&#125;--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectByObject&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;com.bjpowernode.domain.Student&quot;</span>&gt;</span></span><br><span class="line">        select id,name,email,age from student where</span><br><span class="line">        name=#&#123;name,javaType=java.lang.String,jdbcType=VARCHAR&#125;</span><br><span class="line">        or</span><br><span class="line">        age=#&#123;age,javaType=java.lang.Integer,jdbcType=INTEGER&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h3 id="3-2-5-dao接口中多个简单类型的参数，使用位置"><a href="#3-2-5-dao接口中多个简单类型的参数，使用位置" class="headerlink" title="3.2.5 dao接口中多个简单类型的参数，使用位置"></a>3.2.5 dao接口中多个简单类型的参数，使用位置</h3><p>参数位置： dao接口中方法的形参列表，从左往右，参数位置是 0 ， 1， 2……</p>
<p>语法格式：#{arg0} ,#{arg1}</p>
<p>dao接口的方法</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">   使用位置，获取参数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function">List&lt;Student&gt; <span class="title">selectByPosition</span><span class="params">(String name,Integer age)</span></span>;</span><br></pre></td></tr></table></figure>



<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">   mybatis版本是 3.5.1</span></span><br><span class="line"><span class="comment">   使用位置获取参数值， dao接口方法是多个简单类型的参数</span></span><br><span class="line"><span class="comment">   语法： #&#123;arg0&#125;, #&#123;arg1&#125;....</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectByPosition&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;com.bjpowernode.domain.Student&quot;</span>&gt;</span></span><br><span class="line">    select id,name,email,age from student where name=#&#123;arg0&#125; or age=#&#123;arg1&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h3 id="3-2-6-dao接口参数是一个Map"><a href="#3-2-6-dao接口参数是一个Map" class="headerlink" title="3.2.6 dao接口参数是一个Map"></a>3.2.6 dao接口参数是一个Map</h3><p>map作为dao接口的参数， 使用 key 获取参数值，mapper文件中，语法格式 #{key}</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">   使用Map作为参数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function">List&lt;Student&gt; <span class="title">selectStudentByMap</span><span class="params">(Map&lt;String,Object&gt; map)</span></span>;</span><br></pre></td></tr></table></figure>



<p>mapper文件</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">   使用Map传递参数，</span></span><br><span class="line"><span class="comment">   在mapper文件中，获取map的值，是通过key获取的，语法：#&#123;key&#125;</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectStudentByMap&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;com.bjpowernode.domain.Student&quot;</span>&gt;</span></span><br><span class="line">     select id,name,email,age from student where name=#&#123;myname&#125; or age=#&#123;myage&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>



<p>测试，调用方法的位置</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testSelectByMap</span><span class="params">()</span></span>&#123;</span><br><span class="line">    SqlSession sqlSession = MyBatisUtil.getSqlSession();</span><br><span class="line">    StudentDao dao  = sqlSession.getMapper(StudentDao.class);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//使用map传递参数</span></span><br><span class="line">    Map&lt;String,Object&gt; data = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">    data.put(<span class="string">&quot;myname&quot;</span>, <span class="string">&quot;李思思&quot;</span>);</span><br><span class="line">    data.put(<span class="string">&quot;myage&quot;</span>, <span class="number">20</span>);</span><br><span class="line">    List&lt;Student&gt; students = dao.selectStudentByMap(data);</span><br><span class="line"></span><br><span class="line">    students.forEach( stu-&gt; System.out.println(<span class="string">&quot;stu=&quot;</span>+stu));</span><br><span class="line">    sqlSession.close();</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="3-3-和-的区别"><a href="#3-3-和-的区别" class="headerlink" title="3.3 #和$的区别"></a>3.3 #和$的区别</h2><h3 id="3-3-1-占位符"><a href="#3-3-1-占位符" class="headerlink" title="3.3.1 # 占位符"></a>3.3.1 # 占位符</h3><p>语法： #{字符}</p>
<p>mybatis处理#{} 使用jdbc对象是 PrepareStatment对象</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectById&quot;</span>  <span class="attr">parameterType</span>=<span class="string">&quot;integer&quot;</span></span></span><br><span class="line"><span class="tag">             <span class="attr">resultType</span>=<span class="string">&quot;com.bjpowernode.domain.Student&quot;</span>&gt;</span></span><br><span class="line">    select id,name,email,age from student where id=#&#123;studentId&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line"></span><br><span class="line">mybatis出创建PrepareStatement对象，执行sql语句</span><br><span class="line">String sql=&quot; select id,name,email,age from student where id=?&quot;;</span><br><span class="line">PrepareStatement pst = conn.prepareStatement(sql);</span><br><span class="line">pst.setInt(1,1001);  //传递参数</span><br><span class="line">ResultSet rs  = pst.executeQuery(); //执行sql语句</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>



<p>#{}特点：</p>
<p>1）使用的PrepareStatement对象，执行sql语句，效率高。</p>
<p>2）使用的PrepareStatement对象，能避免sql语句， sql语句执行更安全。</p>
<p>3） #{} 常常作为 列值使用的， 位于等号的右侧，  #{}位置的值和数据类型有关的。</p>
<h3 id="3-3-2-占位符"><a href="#3-3-2-占位符" class="headerlink" title="3.3.2 $ 占位符"></a>3.3.2 $ 占位符</h3><p>语法 :  ${字符}</p>
<p>mybatis执行${}占位符的sql语句</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectById&quot;</span>  <span class="attr">parameterType</span>=<span class="string">&quot;integer&quot;</span></span></span><br><span class="line"><span class="tag">             <span class="attr">resultType</span>=<span class="string">&quot;com.bjpowernode.domain.Student&quot;</span>&gt;</span></span><br><span class="line">    select id,name,email,age from student where id=$&#123;studentId&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span>	</span><br><span class="line"></span><br><span class="line">$&#123;&#125; 表示字符串连接， 把sql语句的其他内容和 $&#123;&#125;内容使用 字符串（+） 连接的方式连在一起</span><br><span class="line">String sql=&quot;select id,name,email,age from student where id=&quot; + &quot;1001&quot;;</span><br><span class="line"></span><br><span class="line">mybatis创建Statement对象， 执行sql语句。</span><br><span class="line">Statement stmt  = conn.createStatement(sql);</span><br><span class="line">ResultSet rs  = stmt.executeQuery();</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<p>${} 的特点</p>
<p>1）使用Statement对象，执行sql语句，效率低</p>
<p>2）${}占位符的值，使用的字符串连接方式， 有sql注入的风险。 有代码安全的问题</p>
<ol start="3">
<li> ${} 数据是原样使用的， 不会区分数据类型。</li>
</ol>
<p>4）${} 常用作 表名或者列名， 在能保证数据安全的情况下使用 ${}</p>
<h2 id="3-4-封装MyBatis输出结果"><a href="#3-4-封装MyBatis输出结果" class="headerlink" title="3.4 封装MyBatis输出结果"></a>3.4 封装MyBatis输出结果</h2><p>封装输出结果： MyBatis执行sql语句，得到ResultSet, 转为java对象。 </p>
<p>讲两个 resultType, resultMap</p>
<h3 id="3-4-1-resultType"><a href="#3-4-1-resultType" class="headerlink" title="3.4.1 resultType"></a>3.4.1 resultType</h3><p>resultType属性： 在执行select时使用， 作为<select>标签的属性出现的。</select></p>
<p>resultType:表示结果类型 ，  mysql执行sql语句，得到java对象的类型。  它的值有两种</p>
<p>​             1） java类型的全限定名称  。 2）使用别名</p>
<p>1） resultType:表示java自定义对象</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line">Student selectById(Integer id);</span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectById&quot;</span>  <span class="attr">parameterType</span>=<span class="string">&quot;integer&quot;</span></span></span><br><span class="line"><span class="tag">             <span class="attr">resultType</span>=<span class="string">&quot;com.bjpowernode.domain.Student&quot;</span>&gt;</span></span><br><span class="line">    select id,name,email,age from student where id=#&#123;studentId&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line"></span><br><span class="line">resultType:现在使用java类型的全限定名称。 表示的意思 mybatis执行sql，把ResultSet中的数据转为Student类型的对象。  mybatis会做以下操作：</span><br><span class="line">1. 调用com.bjpowernode.domain.Student的无参数构造方法，创建对象。</span><br><span class="line">    Student student = new Student(); //使用反射创建对象</span><br><span class="line">2. 同名的列赋值给同名的属性。</span><br><span class="line">    student.setId( rs.getInt(&quot;id&quot;));</span><br><span class="line">    student.setName(rs.getString(&quot;name&quot;));</span><br><span class="line">3. 得到java对象， 如果dao接口返回值是List集合， mybatis把student对象放入到List集合。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">所以执行 Student mystudent = dao.selectById(1001); 得到 数据库中 id=1001这行数据， </span><br><span class="line">这行数据的列值， 付给了mystudent对象的属性。 你能得到mystudent对象。 就相当于是 id=1001这行数据。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>



<p>2）resultType表示简单类型</p>
<p>dao方法</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">long</span> <span class="title">countStudent</span><span class="params">()</span></span>;</span><br></pre></td></tr></table></figure>



<p>mapper文件</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">  执行sql语句，得到是一个值（一行一列）</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;countStudent&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;java.lang.Long&quot;</span>&gt;</span></span><br><span class="line">    select count(*) from student</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>



<p>3） resultType:表示一个map结构</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//查询结果返回是一个Map</span></span><br><span class="line"><span class="function">Map&lt;Object,Object&gt; <span class="title">selectMap</span><span class="params">(<span class="meta">@Param(&quot;stuid&quot;)</span> Integer id)</span></span>;</span><br></pre></td></tr></table></figure>



<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">   执行sql得到一个Map结构数据， mybatis执行sql，把ResultSet转为map</span></span><br><span class="line"><span class="comment">   sql执行结果， 列名做map的key ， 列值作为value</span></span><br><span class="line"><span class="comment">   sql执行得到是一行记录，转为map结构是正确的。</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">   dao接口返回是一个map， sql语句最多能获取一行记录，多余一行是错误</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectMap&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;java.util.HashMap&quot;</span>&gt;</span></span><br><span class="line">    select id,name,email from student where id != #&#123;stuid&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>



<p>练习题：</p>
<p> 输入一个省份id ，得到 省份id ，省份name， 城市id，城市名称</p>
<p>例如输入 省份id=1</p>
<p>1 河北  1 石家庄</p>
<p>1 河北   2 秦皇岛</p>
<h3 id="3-4-2-resultMap"><a href="#3-4-2-resultMap" class="headerlink" title="3.4.2 resultMap"></a>3.4.2 resultMap</h3><p>resultMap: 结果映射。 自定义列名和java对象属性的对应关系。 常用在列名和属性名不同的情况。</p>
<p>用法：</p>
<p>1.先定义 resultMap标签， 指定列名和属性名称对应关系</p>
<p>2.在select标签使用resultMap属性，指定上面定义的resultMap的id值</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!--使用resultMap定义列和属性的关系--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--定义resultMap</span></span><br><span class="line"><span class="comment">    id:给resultMap的映射关系起个名称，唯一值</span></span><br><span class="line"><span class="comment">    type:java类型的全限定名称</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">&quot;customMap&quot;</span> <span class="attr">type</span>=<span class="string">&quot;com.bjpowernode.vo.CustomObject&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--定义列名和属性名的对应--&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--主键类型使用id标签--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">id</span> <span class="attr">column</span>=<span class="string">&quot;id&quot;</span> <span class="attr">property</span>=<span class="string">&quot;cid&quot;</span> /&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--非主键类型使用result标签--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;name&quot;</span> <span class="attr">property</span>=<span class="string">&quot;cname&quot;</span> /&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--列名和属性名相同不用定义--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;email&quot;</span> <span class="attr">property</span>=<span class="string">&quot;email&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;age&quot;</span> <span class="attr">property</span>=<span class="string">&quot;age&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!--使用resultMap属性，指定映射关系的id</span></span><br><span class="line"><span class="comment">    resultMap和resultType 不能同时使用， 二选一。</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectById2&quot;</span> <span class="attr">resultMap</span>=<span class="string">&quot;customMap&quot;</span>&gt;</span></span><br><span class="line">  select id,name,email,age from student where id=#&#123;stuid&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h2 id="3-5-自定义别名"><a href="#3-5-自定义别名" class="headerlink" title="3.5 自定义别名"></a>3.5 自定义别名</h2><p>mybatis提供的对java类型定义简短，好记名称。</p>
<p>自定义别名的步骤：</p>
<p>1）在mybatis主配置文件，使用 typeAliases标签声明别名</p>
<p>2）在mapper文件中， resultType=”别名”</p>
<p>声明别名（mybatis主配置文件）</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">typeAliases</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--第一种语法格式</span></span><br><span class="line"><span class="comment">        type:java类型的全限定名称（自定义类型）</span></span><br><span class="line"><span class="comment">        alias:自定义别名</span></span><br><span class="line"><span class="comment">    --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">typeAlias</span> <span class="attr">type</span>=<span class="string">&quot;com.bjpowernode.domain.Student&quot;</span> <span class="attr">alias</span>=<span class="string">&quot;stu&quot;</span> /&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">typeAliases</span>&gt;</span></span><br></pre></td></tr></table></figure>



<p>mapper文件中使用</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line">resultType=&quot;别名&quot;</span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectById&quot;</span>  <span class="attr">parameterType</span>=<span class="string">&quot;integer&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;stu&quot;</span>&gt;</span></span><br><span class="line">    select id,name,email,age from student where id=#&#123;studentId&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h2 id="3-6-列名和java对象属性名称不一样解决方式"><a href="#3-6-列名和java对象属性名称不一样解决方式" class="headerlink" title="3.6 列名和java对象属性名称不一样解决方式"></a>3.6 列名和java对象属性名称不一样解决方式</h2><p>1） 使用resultMap： 自定义列名和属性名称对应关系</p>
<p>2）使用resultType:  使用列别名，让别名和java对象属性名称一样</p>
<h2 id="3-7-like"><a href="#3-7-like" class="headerlink" title="3.7 like"></a>3.7 like</h2><p>第一种方式： 在java程序中，把like的内容组装好。 把这个内容传入到sql语句</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//like第一种方式</span></span><br><span class="line"><span class="function">List&lt;Student&gt; <span class="title">selectLikeOne</span><span class="params">(<span class="meta">@Param(&quot;name&quot;)</span> String name)</span></span>;</span><br></pre></td></tr></table></figure>



<p>mapper</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!--like第一种方式--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectLikeOne&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;com.bjpowernode.domain.Student&quot;</span>&gt;</span></span><br><span class="line">    select * from student where name like #&#123;name&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>



<p>执行like</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testLikeOne</span><span class="params">()</span></span>&#123;</span><br><span class="line">    SqlSession sqlSession = MyBatisUtil.getSqlSession();</span><br><span class="line">    StudentDao dao  = sqlSession.getMapper(StudentDao.class);</span><br><span class="line"></span><br><span class="line">    String name=<span class="string">&quot;%李%&quot;</span>;</span><br><span class="line">    List&lt;Student&gt; students = dao.selectLikeOne(name);</span><br><span class="line"></span><br><span class="line">    sqlSession.close();</span><br><span class="line"></span><br><span class="line">    students.forEach( stu-&gt; System.out.println(stu));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>第二种方式： 在sql语句，组织like的内容。 </p>
<p>sql语句like的格式：  where name like “%”空格#{name}空格”%”</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//like第二种方式</span></span><br><span class="line"><span class="function">List&lt;Student&gt; <span class="title">selectLikeTwo</span><span class="params">(<span class="meta">@Param(&quot;name&quot;)</span> String name)</span></span>;</span><br></pre></td></tr></table></figure>



<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!--like第二种方式--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectLikeTwo&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;com.bjpowernode.domain.Student&quot;</span>&gt;</span></span><br><span class="line">    select * from student where name like &quot;%&quot; #&#123;name&#125; &quot;%&quot;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>



<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testLikeTwo</span><span class="params">()</span></span>&#123;</span><br><span class="line">    SqlSession sqlSession = MyBatisUtil.getSqlSession();</span><br><span class="line">    StudentDao dao  = sqlSession.getMapper(StudentDao.class);</span><br><span class="line"></span><br><span class="line">    String name=<span class="string">&quot;李&quot;</span>;</span><br><span class="line">    List&lt;Student&gt; students = dao.selectLikeTwo(name);</span><br><span class="line"></span><br><span class="line">    sqlSession.close();</span><br><span class="line"></span><br><span class="line">    students.forEach( stu-&gt; System.out.println(stu));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h1 id="第四章-动态sql"><a href="#第四章-动态sql" class="headerlink" title="第四章 动态sql"></a>第四章 动态sql</h1><p>什么是动态sql： 同一个dao的方法， 根据不同的条件可以表示不同的sql语句， 主要是where部分有变化</p>
<p>使用mybatis提供的标签，实现动态sql的能力，  主要讲 if ，where ，foreach， sql。</p>
<p>使用动态sql的时候， dao方法的形参使用java对象。 </p>
<p>什么时候使用动态sql：</p>
<p><img src="/2022/11/23/mybatis%E5%AD%A6%E4%B9%A0/image-20201020114831474.png" alt="image-20201020114831474">    </p>
<h2 id="4-1-if-标签"><a href="#4-1-if-标签" class="headerlink" title="4.1  if 标签"></a>4.1  if 标签</h2><p>语法：</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;boolean判断结果&quot;</span>&gt;</span></span><br><span class="line">   sql 代码</span><br><span class="line"><span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">在mapper文件中</span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectStudent&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;com.bjpwoernode.domain.Student&quot;</span>&gt;</span></span><br><span class="line">      select *from student</span><br><span class="line">      <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;条件&quot;</span>&gt;</span></span><br><span class="line">          sql语句</span><br><span class="line">      <span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;条件&quot;</span>&gt;</span></span><br><span class="line">          sql语句</span><br><span class="line">      <span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>



<p>例子：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="function">List&lt;Student&gt; <span class="title">selectIf</span><span class="params">(Student student)</span></span>;</span><br></pre></td></tr></table></figure>



<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!--if</span></span><br><span class="line"><span class="comment">    test: 使用对象的属性值作为条件</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectIf&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;com.bjpowernode.domain.Student&quot;</span>&gt;</span></span><br><span class="line">    select * from student</span><br><span class="line">    where id=-1</span><br><span class="line">    <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;name !=null and name!=&#x27;&#x27;&quot;</span>&gt;</span></span><br><span class="line">       or  name = #&#123;name&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;age &gt;0&quot;</span>&gt;</span></span><br><span class="line">        or age <span class="symbol">&amp;lt;</span> #&#123;age&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h2 id="4-2-where-标签"><a href="#4-2-where-标签" class="headerlink" title="4.2 where 标签"></a>4.2 where 标签</h2><p>使用if标签时，容易引起sql语句语法错误。  使用where标签解决if产生的语法问题。</p>
<p>使用时 where ,里面是一个或多个if 标签， 当有一个if标签 判断条件为true，  where标签会转为 WHERE 关键字附加到sql语句的后面。  如果 if 没有一个条件为true ， 忽略where和里面的if。</p>
<p>where标签删除 和他最近的or 或者 and。</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line">语法：</span><br><span class="line"><span class="tag">&lt;<span class="name">where</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;条件1&quot;</span>&gt;</span>sql语句1<span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;条件2&quot;</span>&gt;</span>sql语句2<span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">where</span>&gt;</span></span><br></pre></td></tr></table></figure>



<p>例子：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//where</span></span><br><span class="line"><span class="function">List&lt;Student&gt; <span class="title">selectWhere</span><span class="params">(Student student)</span></span>;</span><br></pre></td></tr></table></figure>



<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!--where--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectWhere&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;com.bjpowernode.domain.Student&quot;</span>&gt;</span></span><br><span class="line">    select * from student</span><br><span class="line">    <span class="tag">&lt;<span class="name">where</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;name !=null and name!=&#x27;&#x27;&quot;</span>&gt;</span></span><br><span class="line">            or  name = #&#123;name&#125;</span><br><span class="line">        <span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;age &gt;0&quot;</span>&gt;</span></span><br><span class="line">            or age <span class="symbol">&amp;lt;</span> #&#123;age&#125;</span><br><span class="line">        <span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">where</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h2 id="4-3-foreach-循环"><a href="#4-3-foreach-循环" class="headerlink" title="4.3 foreach 循环"></a>4.3 foreach 循环</h2><p>使用foreach可以循环数组，list集合， 一般使用在in语句中。</p>
<p>语法：</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line">&lt; foreach collection=&quot;集合类型&quot; open=&quot;开始的字符&quot; close=&quot;结束的字符&quot;</span><br><span class="line">          item=&quot;集合中的成员&quot; separator=&quot;集合成员之间的分隔符&quot;&gt;</span><br><span class="line">              #&#123;item 的值&#125;</span><br><span class="line">&lt;/ foreach&gt;</span><br><span class="line"></span><br><span class="line">标签属性：</span><br><span class="line">collection： 表示，循环的对象是 数组， 还是list集合。  如果dao接口方法的形参是 数组， </span><br><span class="line">             collection=&quot;array&quot; ,如果dao接口形参是List， collection=&quot;list&quot;</span><br><span class="line"></span><br><span class="line">open:循环开始时的字符。  sql.append(&quot;(&quot;);</span><br><span class="line">close：循环结束时字符。  sql.append(&quot;)&quot;);</span><br><span class="line">item：集合成员， 自定义的变量。   Integer item  = idlist.get(i);// item是集合成员</span><br><span class="line">separator：集合成员之间的分隔符。  sql.append(&quot;,&quot;); //集合成员之间的分隔符</span><br><span class="line">#&#123;item 的值&#125;：获取集合成员的值。</span><br></pre></td></tr></table></figure>



<p>第一种方式：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//foreach-1</span></span><br><span class="line"><span class="function">List&lt;Student&gt; <span class="title">selectForeachOne</span><span class="params">(List&lt;Integer&gt; idlist)</span></span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&lt;!--foreach第一种方式， 循环简单类型的List--&gt;</span><br><span class="line">&lt;select id=<span class="string">&quot;selectForeachOne&quot;</span> resultType=<span class="string">&quot;com.bjpowernode.domain.Student&quot;</span>&gt;</span><br><span class="line">      select * from student</span><br><span class="line">        &lt;<span class="keyword">if</span> test=<span class="string">&quot;list !=null and list.size&gt;0&quot;</span>&gt;</span><br><span class="line">            where id in</span><br><span class="line">            &lt;foreach collection=<span class="string">&quot;list&quot;</span> open=<span class="string">&quot;(&quot;</span> close=<span class="string">&quot;)&quot;</span> separator=<span class="string">&quot;,&quot;</span> item=<span class="string">&quot;myid&quot;</span>&gt;</span><br><span class="line">                  #&#123;myid&#125;</span><br><span class="line">            &lt;/foreach&gt;</span><br><span class="line">        &lt;/<span class="keyword">if</span>&gt;</span><br><span class="line">&lt;/select&gt;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testSelectForeachOne</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">//1.获取SqlSession</span></span><br><span class="line">        SqlSession session = MyBatisUtil.getSqlSession();</span><br><span class="line">        <span class="comment">//2.获取dao的代理</span></span><br><span class="line">        StudentDao dao = session.getMapper(StudentDao.class);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        List&lt;Integer&gt; idlist = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">        idlist.add(<span class="number">1001</span>);</span><br><span class="line">        idlist.add(<span class="number">1002</span>);</span><br><span class="line">        idlist.add(<span class="number">1003</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        List&lt;Student&gt; students  = dao.selectForeachOne(idlist);</span><br><span class="line"></span><br><span class="line">        students.forEach( stu-&gt; System.out.println(<span class="string">&quot;stu==&quot;</span>+stu));</span><br><span class="line">        <span class="comment">//3.关闭SqlSession对象</span></span><br><span class="line">        session.close();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<p>第二种方式：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//foreach-2</span></span><br><span class="line"><span class="function">List&lt;Student&gt; <span class="title">selectForeachTwo</span><span class="params">(List&lt;Student&gt; studentList)</span></span>;</span><br><span class="line"></span><br><span class="line">&lt;!--foreach第二种方式， 循环的List&lt;Student&gt;--&gt;</span><br><span class="line">&lt;select id=<span class="string">&quot;selectForeachTwo&quot;</span> resultType=<span class="string">&quot;com.bjpowernode.domain.Student&quot;</span>&gt;</span><br><span class="line">        select * from student</span><br><span class="line">        &lt;<span class="keyword">if</span> test=<span class="string">&quot;list != null and list.size&gt;0&quot;</span>&gt;</span><br><span class="line">            where id in</span><br><span class="line">            &lt;foreach collection=<span class="string">&quot;list&quot;</span> open=<span class="string">&quot;(&quot;</span> close=<span class="string">&quot;)&quot;</span> separator=<span class="string">&quot;,&quot;</span> item=<span class="string">&quot;stu&quot;</span>&gt;</span><br><span class="line">                  #&#123;stu.id&#125;</span><br><span class="line">            &lt;/foreach&gt;</span><br><span class="line">        &lt;/<span class="keyword">if</span>&gt;</span><br><span class="line">&lt;/select&gt;</span><br><span class="line">            </span><br><span class="line"> <span class="meta">@Test</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testSelectForeachTwo</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">//1.获取SqlSession</span></span><br><span class="line">        SqlSession session = MyBatisUtil.getSqlSession();</span><br><span class="line">        <span class="comment">//2.获取dao的代理</span></span><br><span class="line">        StudentDao dao = session.getMapper(StudentDao.class);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        List&lt;Student&gt; list  = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">        Student s1 = <span class="keyword">new</span> Student();</span><br><span class="line">        s1.setId(<span class="number">1001</span>);</span><br><span class="line"></span><br><span class="line">        Student s2 = <span class="keyword">new</span> Student();</span><br><span class="line">        s2.setId(<span class="number">1002</span>);</span><br><span class="line"></span><br><span class="line">        list.add(s1);</span><br><span class="line">        list.add(s2);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        List&lt;Student&gt; students  = dao.selectForeachTwo(list);</span><br><span class="line"></span><br><span class="line">        students.forEach( stu-&gt; System.out.println(<span class="string">&quot;stu==&quot;</span>+stu));</span><br><span class="line">        <span class="comment">//3.关闭SqlSession对象</span></span><br><span class="line">        session.close();</span><br><span class="line">    &#125;</span><br><span class="line">            </span><br></pre></td></tr></table></figure>

<h2 id="4-4-sql标签"><a href="#4-4-sql标签" class="headerlink" title="4.4 sql标签"></a>4.4 sql标签</h2><p>sql标签标示 一段sql代码， 可以是表名，几个字段， where条件都可以， 可以在其他地方复用sql标签的内容。</p>
<p>使用方式：</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line">1) 在mapper文件中定义 sql代码片段 <span class="tag">&lt;<span class="name">sql</span> <span class="attr">id</span>=<span class="string">&quot;唯一字符串&quot;</span>&gt;</span>  部分sql语句  <span class="tag">&lt;/<span class="name">sql</span>&gt;</span></span><br><span class="line">2）在其他的位置，使用include标签引用某个代码片段</span><br></pre></td></tr></table></figure>



<p>例如：</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!--定义代码片段--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">sql</span> <span class="attr">id</span>=<span class="string">&quot;selectStudent&quot;</span>&gt;</span></span><br><span class="line">    select * from student</span><br><span class="line"><span class="tag">&lt;/<span class="name">sql</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">sql</span> <span class="attr">id</span>=<span class="string">&quot;studentFieldList&quot;</span>&gt;</span></span><br><span class="line">    id,name,email</span><br><span class="line"><span class="tag">&lt;/<span class="name">sql</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectIf&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;com.bjpowernode.domain.Student&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="tag">&lt;<span class="name">include</span> <span class="attr">refid</span>=<span class="string">&quot;selectStudent&quot;</span> /&gt;</span></span><br><span class="line"></span><br><span class="line">        where id=-1</span><br><span class="line">        <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;name !=null and name!=&#x27;&#x27;&quot;</span>&gt;</span></span><br><span class="line">           or  name = #&#123;name&#125;</span><br><span class="line">        <span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;age &gt;0&quot;</span>&gt;</span></span><br><span class="line">            or age <span class="symbol">&amp;lt;</span> #&#123;age&#125;</span><br><span class="line">        <span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--where--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectWhere&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;com.bjpowernode.domain.Student&quot;</span>&gt;</span></span><br><span class="line">        select <span class="tag">&lt;<span class="name">include</span> <span class="attr">refid</span>=<span class="string">&quot;studentFieldList&quot;</span>/&gt;</span> from student</span><br><span class="line">        <span class="tag">&lt;<span class="name">where</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;name !=null and name!=&#x27;&#x27;&quot;</span>&gt;</span></span><br><span class="line">                or  name = #&#123;name&#125;</span><br><span class="line">            <span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line"></span><br><span class="line">            <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;age &gt;0&quot;</span>&gt;</span></span><br><span class="line">                or age <span class="symbol">&amp;lt;</span> #&#123;age&#125;</span><br><span class="line">            <span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">where</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h1 id="第五章-MyBatis配置文件"><a href="#第五章-MyBatis配置文件" class="headerlink" title="第五章 MyBatis配置文件"></a>第五章 MyBatis配置文件</h1><p>mybatis配置文件两大类： 1 mybatis主配置文件； 2 mybatis的mapper文件</p>
<ol>
<li>mybatis主配置文件，提供mybatis全局设置的。包含的内容 日志， 数据源，mapper文件位置</li>
<li>mapper文件： 写sql语句的。 一个表一个mapper文件</li>
</ol>
<h2 id="5-1-settings部分"><a href="#5-1-settings部分" class="headerlink" title="5.1 settings部分"></a>5.1 settings部分</h2><p>settings是mybatis的全局设置，影响整个mybatis的运行。 这个设置一般使用默认值就可以了。</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">settings</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;cacheEnabled&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;lazyLoadingEnabled&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;multipleResultSetsEnabled&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;useColumnLabel&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;useGeneratedKeys&quot;</span> <span class="attr">value</span>=<span class="string">&quot;false&quot;</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;autoMappingBehavior&quot;</span> <span class="attr">value</span>=<span class="string">&quot;PARTIAL&quot;</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;autoMappingUnknownColumnBehavior&quot;</span> <span class="attr">value</span>=<span class="string">&quot;WARNING&quot;</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;defaultExecutorType&quot;</span> <span class="attr">value</span>=<span class="string">&quot;SIMPLE&quot;</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;defaultStatementTimeout&quot;</span> <span class="attr">value</span>=<span class="string">&quot;25&quot;</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;defaultFetchSize&quot;</span> <span class="attr">value</span>=<span class="string">&quot;100&quot;</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;safeRowBoundsEnabled&quot;</span> <span class="attr">value</span>=<span class="string">&quot;false&quot;</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;mapUnderscoreToCamelCase&quot;</span> <span class="attr">value</span>=<span class="string">&quot;false&quot;</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;localCacheScope&quot;</span> <span class="attr">value</span>=<span class="string">&quot;SESSION&quot;</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;jdbcTypeForNull&quot;</span> <span class="attr">value</span>=<span class="string">&quot;OTHER&quot;</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;lazyLoadTriggerMethods&quot;</span> <span class="attr">value</span>=<span class="string">&quot;equals,clone,hashCode,toString&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">settings</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="5-2-typeAliase-别名"><a href="#5-2-typeAliase-别名" class="headerlink" title="5.2 typeAliase 别名"></a>5.2 typeAliase 别名</h2><p>设置别名</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">typeAliases</span>&gt;</span></span><br><span class="line">       <span class="comment">&lt;!--第一种语法格式</span></span><br><span class="line"><span class="comment">           type:java类型的全限定名称（自定义类型）</span></span><br><span class="line"><span class="comment">           alias:自定义别名</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">           优点： 别名可以自定义</span></span><br><span class="line"><span class="comment">           缺点： 每个类型必须单独定义</span></span><br><span class="line"><span class="comment">       --&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">typeAlias</span> <span class="attr">type</span>=<span class="string">&quot;com.bjpowernode.domain.Student&quot;</span> <span class="attr">alias</span>=<span class="string">&quot;stu&quot;</span> /&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">typeAlias</span> <span class="attr">type</span>=<span class="string">&quot;com.bjpowernode.vo.QueryParam&quot;</span> <span class="attr">alias</span>=<span class="string">&quot;qp&quot;</span> /&gt;</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">&lt;!--第二种方式</span></span><br><span class="line"><span class="comment">           name:包名， mybatis会把这个包中所有类名作为别名（不用区分大小写）</span></span><br><span class="line"><span class="comment">           优点：使用方便，一次给多个类定义别名</span></span><br><span class="line"><span class="comment">           缺点: 别名不能自定义，必须是类名。</span></span><br><span class="line"><span class="comment">       --&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">package</span> <span class="attr">name</span>=<span class="string">&quot;com.bjpowernode.domain&quot;</span> /&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">package</span> <span class="attr">name</span>=<span class="string">&quot;com.bjpowernode.vo&quot;</span> /&gt;</span></span><br><span class="line"></span><br><span class="line">   <span class="tag">&lt;/<span class="name">typeAliases</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h2 id="5-3-配置环境"><a href="#5-3-配置环境" class="headerlink" title="5.3 配置环境"></a>5.3 配置环境</h2><figure class="highlight xml"><table><tr><td class="code"><pre><span class="line">environments: 环境标签， 在他里面可以配置多个environment</span><br><span class="line">       属性： default ，必须是某个environment的id属性值。 表示mybatis默认连接的数据库</span><br><span class="line">environment： 表示一个数据库的连接信息。</span><br><span class="line">       属性： id 自定义的环境的标识。 唯一值。</span><br><span class="line">transactionManager：事务管理器</span><br><span class="line">       属性：  type 表示事务管理器的类型。</span><br><span class="line">       属性值：1）JDBC: 使用Connection对象， 由mybatis自己完成事务的处理。</span><br><span class="line">             2） MANAGED: 管理，表示把事务的处理交给容器实现（由其他软件完成事务的提交，回滚）</span><br><span class="line">dataSource： 数据源，创建的Connection对象，连接数据库。</span><br><span class="line">       属性：  type 数据源的类型</span><br><span class="line">       属性值：1） POOLED， mybatis会在内存中创建PooledDataSource类，管理多个Connection连接对象，使                           用的连接池</span><br><span class="line">             2） UNPOOLED ，不使用连接池， mybatis创建一个UnPooledDataSource这个类， 每次执行sql                           语句先创建Connection对象，再执行sql语句，最后关闭Connection</span><br><span class="line">             3) JNDI : java的命名和目录服务。</span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">environments</span> <span class="attr">default</span>=<span class="string">&quot;online&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">environment</span> <span class="attr">id</span>=<span class="string">&quot;development&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">transactionManager</span> <span class="attr">type</span>=<span class="string">&quot;JDBC&quot;</span>/&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--配置数据源： 创建Connection对象。--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">dataSource</span> <span class="attr">type</span>=<span class="string">&quot;POOLED&quot;</span>&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--driver:驱动的内容--&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;driver&quot;</span> <span class="attr">value</span>=<span class="string">&quot;com.mysql.jdbc.Driver&quot;</span>/&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--连接数据库的url--&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;url&quot;</span></span></span><br><span class="line"><span class="tag">                          <span class="attr">value</span>=<span class="string">&quot;jdbc:mysql://localhost:3306/springdb&quot;</span>/&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--用户名--&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;username&quot;</span> <span class="attr">value</span>=<span class="string">&quot;root&quot;</span>/&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--密码--&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;password&quot;</span> <span class="attr">value</span>=<span class="string">&quot;123&quot;</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">dataSource</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">environment</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 项目上线后使用的数据库 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">environment</span> <span class="attr">id</span>=<span class="string">&quot;online&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">transactionManager</span> <span class="attr">type</span>=<span class="string">&quot;JDBC&quot;</span>/&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--配置数据源： 创建Connection对象。--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">dataSource</span> <span class="attr">type</span>=<span class="string">&quot;POOLED&quot;</span>&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--driver:驱动的内容--&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;driver&quot;</span> <span class="attr">value</span>=<span class="string">&quot;com.mysql.jdbc.Driver&quot;</span>/&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--连接数据库的url--&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;url&quot;</span></span></span><br><span class="line"><span class="tag">                          <span class="attr">value</span>=<span class="string">&quot;jdbc:mysql://localhost:3306/springdb&quot;</span>/&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--用户名--&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;username&quot;</span> <span class="attr">value</span>=<span class="string">&quot;admin&quot;</span>/&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--密码--&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;password&quot;</span> <span class="attr">value</span>=<span class="string">&quot;123456&quot;</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">dataSource</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">environment</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">environments</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h2 id="5-4-使用数据库属性配置文件（-）"><a href="#5-4-使用数据库属性配置文件（-）" class="headerlink" title="5.4 使用数据库属性配置文件（*）"></a>5.4 使用数据库属性配置文件（*）</h2><p>需要把数据库的配置信息放到一个单独文件中， 独立管理。 这个文件扩展名是 properties. 在这个文件中，使用自定义的key=value的格式表示数据</p>
<p>使用步骤：</p>
<p>1.在resources目录中，创建xxxx.properties</p>
<p>2.在文件中，使用 key=value的格式定义数据。</p>
<p>   例如 jdbc.url=jdbc:mysq://localhost:3306/springdb</p>
<p>3.在mybatis主配置文件， 使用properties标签引用外部的属性配置文件</p>
<p>4.在使用值的位置， 使用${key}获取key对应的value（等号右侧的值）</p>
<p>例子：</p>
<p>jdbc.properties</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line">jdbc.driver=com.mysql.jdbc.Driver</span><br><span class="line">jdbc.url=jdbc:mysql://localhost:3306/springdb?useUnicode=true<span class="symbol">&amp;amp;</span>characterEncoding=utf-8</span><br><span class="line">jdbc.username=root</span><br><span class="line">jdbc.password=123</span><br></pre></td></tr></table></figure>



<p>mybatis主配置文件</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!--使用外部属性配置文件</span></span><br><span class="line"><span class="comment">    resource:指定类路径下的某个属性配置文件</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">properties</span> <span class="attr">resource</span>=<span class="string">&quot;jdbc.properties&quot;</span> /&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">environments</span> <span class="attr">default</span>=<span class="string">&quot;development&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">environment</span> <span class="attr">id</span>=<span class="string">&quot;development&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">transactionManager</span> <span class="attr">type</span>=<span class="string">&quot;JDBC&quot;</span>/&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--配置数据源： 创建Connection对象。--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">dataSource</span> <span class="attr">type</span>=<span class="string">&quot;POOLED&quot;</span>&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--driver:驱动的内容--&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;driver&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;jdbc.driver&#125;&quot;</span>/&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--连接数据库的url--&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;url&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;jdbc.url&#125;&quot;</span>/&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--用户名--&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;username&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;jdbc.username&#125;&quot;</span>/&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--密码--&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;password&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;jdbc.password&#125;&quot;</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">dataSource</span>&gt;</span></span><br><span class="line">         <span class="tag">&lt;/<span class="name">environment</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">environments</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="5-5-mapper-标签"><a href="#5-5-mapper-标签" class="headerlink" title="5.5 mapper 标签(*)"></a>5.5 mapper 标签(*)</h2><p>使用mapper指定其他mapper文件的位置， </p>
<p>mapper标签使用的格式有两个常用的方式：</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">mappers</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--第一种方式， resources=&quot;mapper文件的路径&quot;</span></span><br><span class="line"><span class="comment">        优点：文件清晰。 加载的文件是明确的。</span></span><br><span class="line"><span class="comment">              文件的位置比较灵活。</span></span><br><span class="line"><span class="comment">        缺点：文件比较多， 代码量会比较大， 管理难度大</span></span><br><span class="line"><span class="comment">    --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">mapper</span> <span class="attr">resource</span>=<span class="string">&quot;com/bjpowernode/dao/StudentDao.xml&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">mapper</span> <span class="attr">resource</span>=<span class="string">&quot;com/bjpowernode/dao/OrderDao.xml&quot;</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">       第二种方式，使用&lt;package&gt;</span></span><br><span class="line"><span class="comment">       name:包名， mapper文件所在的包名。</span></span><br><span class="line"><span class="comment">       特点： 把这个包中的所有mapper文件，一次加载。</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">       使用要求：</span></span><br><span class="line"><span class="comment">        1. mapper文件和dao接口在同一目录</span></span><br><span class="line"><span class="comment">        2. mapper文件和dao接口名称完全一样。</span></span><br><span class="line"><span class="comment">    --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">package</span> <span class="attr">name</span>=<span class="string">&quot;com.bjpowernode.dao&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">package</span> <span class="attr">name</span>=<span class="string">&quot;com.bjpowernode.dao1&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">mappers</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h1 id="第六章-PageHelper"><a href="#第六章-PageHelper" class="headerlink" title="第六章 PageHelper"></a>第六章 PageHelper</h1><p>PageHelper做数据分页。 在你的select语句后面加入 分页的 sql 内容， 如果你使用的mysql数据库， 它就是在select * from student 后面加入 limit 语句。</p>
<p>使用步骤：</p>
<p>1.加入依赖pagehelper依赖</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.github.pagehelper<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>pagehelper<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">version</span>&gt;</span>5.1.10<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>



<p>2.在mybatis主配置文件， 加入plugin声明</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line">在<span class="tag">&lt;<span class="name">environments</span>&gt;</span> 之前加入</span><br><span class="line"><span class="tag">&lt;<span class="name">plugins</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">plugin</span> <span class="attr">interceptor</span> =<span class="string">&quot;com.github.pagehelper.PageInterceptor&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">plugins</span>&gt;</span>    </span><br></pre></td></tr></table></figure>



<p>3.在select语句之前，调用PageHelper.startPage(页码， 每页大小)</p>
<p>对比：</p>
<p>没有使用PageHelper</p>
<p>select * from student order by id</p>
<p>使用PageHelper</p>
<p>SELECT count(0) FROM student </p>
<p>select * from student order by id LIMIT ? </p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://code-ran.gitee.io">牛牛</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://code-ran.gitee.io/2022/11/23/mybatis%E5%AD%A6%E4%B9%A0/">https://code-ran.gitee.io/2022/11/23/mybatis%E5%AD%A6%E4%B9%A0/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://code-ran.gitee.io" target="_blank">牛牛&blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/MyBatis/">MyBatis</a></div><div class="post_share"><div class="social-share" data-image="" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/11/23/java8%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/"><img class="prev-cover" src="" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">java8函数式编程</div></div></a></div><div class="next-post pull-right"><a href="/2021/10/31/elementui%E4%B8%ADinput%E8%BE%93%E5%85%A5%E6%A1%86%EF%BC%8C%E5%BC%BA%E5%88%B6%E8%BE%93%E5%85%A5%E6%95%B0%E5%AD%97%EF%BC%8C%E5%B9%B6%E9%99%90%E5%88%B6%E8%BE%93%E5%85%A5%E9%95%BF%E5%BA%A6/"><img class="next-cover" src="" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">elementui中input输入框，强制输入数字，并限制输入长度</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2022/11/23/Mybatis%E4%B8%ADCDATA%E7%9A%84%E4%BD%BF%E7%94%A8/" title="Mybatis中 &lt;![CDATA[ ]]&gt; 的使用"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-11-23</div><div class="title">Mybatis中 &lt;![CDATA[ ]]&gt; 的使用</div></div></a></div><div><a href="/2022/11/23/mybatis%E5%85%A8%E5%8A%A8%E6%80%81%E6%8F%92%E5%85%A5/" title="MyBatis全动态插入"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-11-23</div><div class="title">MyBatis全动态插入</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/ys.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">牛牛</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">52</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">26</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">26</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/code-ran"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">本blog内容仅用于个人学习使用!</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#MyBatis-%E6%A1%86%E6%9E%B6"><span class="toc-number">1.</span> <span class="toc-text">MyBatis 框架</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AC%AC%E4%B8%80%E7%AB%A0-%E6%A1%86%E6%9E%B6%E7%9A%84%E6%A6%82%E8%BF%B0"><span class="toc-number">2.</span> <span class="toc-text">第一章 框架的概述</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E4%B8%89%E5%B1%82%E6%9E%B6%E6%9E%84"><span class="toc-number">2.1.</span> <span class="toc-text">1.三层架构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E4%B8%89%E5%B1%82%E6%9E%B6%E6%9E%84%E8%AF%B7%E6%B1%82%E7%9A%84%E5%A4%84%E7%90%86%E6%B5%81%E7%A8%8B"><span class="toc-number">2.2.</span> <span class="toc-text">2. 三层架构请求的处理流程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E4%B8%BA%E4%BB%80%E4%B9%88%E8%A6%81%E4%BD%BF%E7%94%A8%E4%B8%89%E5%B1%82%EF%BC%9F"><span class="toc-number">2.3.</span> <span class="toc-text">3. 为什么要使用三层？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-%E4%B8%89%E5%B1%82%E6%9E%B6%E6%9E%84%E6%A8%A1%E5%BC%8F%E5%92%8C%E6%A1%86%E6%9E%B6"><span class="toc-number">2.4.</span> <span class="toc-text">4. 三层架构模式和框架</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-%E6%A1%86%E6%9E%B6"><span class="toc-number">2.5.</span> <span class="toc-text">5 .框架</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-%E6%A1%86%E6%9E%B6%E8%A7%A3%E5%86%B3%E7%9A%84%E9%97%AE%E9%A2%98"><span class="toc-number">2.6.</span> <span class="toc-text">6. 框架解决的问题</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7-jdbc%E8%AE%BF%E9%97%AE%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E4%BC%98%E7%BC%BA%E7%82%B9"><span class="toc-number">2.7.</span> <span class="toc-text">7. jdbc访问数据库的优缺点</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8-MyBatis%E6%A1%86%E6%9E%B6"><span class="toc-number">2.8.</span> <span class="toc-text">8 MyBatis框架</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AC%AC%E4%BA%8C%E7%AB%A0-MyBatis%E5%85%A5%E9%97%A8"><span class="toc-number">3.</span> <span class="toc-text">第二章  MyBatis入门</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-%E7%AC%AC%E4%B8%80%E4%B8%AA%E4%BE%8B%E5%AD%90"><span class="toc-number">3.1.</span> <span class="toc-text">2.1 第一个例子</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-%E6%A6%82%E5%BF%B5"><span class="toc-number">3.2.</span> <span class="toc-text">2.2 概念</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-MyBatis%E7%9A%84%E4%B8%80%E4%BA%9B%E9%87%8D%E8%A6%81%E5%AF%B9%E8%B1%A1"><span class="toc-number">3.3.</span> <span class="toc-text">2.3 MyBatis的一些重要对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-4-%E4%BD%BF%E7%94%A8%E5%B7%A5%E5%85%B7%E7%B1%BB%E5%92%8C%E6%A8%A1%E7%89%88"><span class="toc-number">3.4.</span> <span class="toc-text">2.4 使用工具类和模版</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AC%AC%E4%B8%89%E7%AB%A0-MyBatis%E7%9A%84Dao%E4%BB%A3%E7%90%86"><span class="toc-number">4.</span> <span class="toc-text">第三章  MyBatis的Dao代理</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-1-dao%E4%BB%A3%E7%90%86"><span class="toc-number">4.1.</span> <span class="toc-text">3.1  dao代理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1-1-mybatis%E6%8F%90%E4%BE%9B%E4%BB%A3%E7%90%86%EF%BC%9A"><span class="toc-number">4.1.1.</span> <span class="toc-text">3.1.1 mybatis提供代理：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1-2-%E4%BD%BF%E7%94%A8mybatis%E4%BB%A3%E7%90%86%E8%A6%81%E6%B1%82"><span class="toc-number">4.1.2.</span> <span class="toc-text">3.1.2 使用mybatis代理要求</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1-3-mybatis%E4%BB%A3%E7%90%86%E5%AE%9E%E7%8E%B0%E6%96%B9%E5%BC%8F"><span class="toc-number">4.1.3.</span> <span class="toc-text">3.1.3 mybatis代理实现方式</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2-%E7%90%86%E8%A7%A3%E5%8F%82%E6%95%B0"><span class="toc-number">4.2.</span> <span class="toc-text">3.2 理解参数</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-1-parameterType"><span class="toc-number">4.2.1.</span> <span class="toc-text">3.2.1 parameterType</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-2-dao%E6%8E%A5%E5%8F%A3%E6%96%B9%E6%B3%95%E6%98%AF%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%B1%BB%E5%9E%8B%E7%9A%84%E5%8F%82%E6%95%B0"><span class="toc-number">4.2.2.</span> <span class="toc-text">3.2.2 dao接口方法是一个简单类型的参数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-3-dao%E6%8E%A5%E5%8F%A3%E6%96%B9%E6%B3%95%E6%9C%89%E5%A4%9A%E4%B8%AA%E7%AE%80%E5%8D%95%E7%B1%BB%E5%9E%8B%E7%9A%84%E5%8F%82%E6%95%B0"><span class="toc-number">4.2.3.</span> <span class="toc-text">3.2.3 dao接口方法有多个简单类型的参数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-4-dao%E6%8E%A5%E5%8F%A3%E6%96%B9%E6%B3%95%E4%BD%BF%E7%94%A8%E4%B8%80%E4%B8%AA%E5%AF%B9%E8%B1%A1%E4%BD%9C%E4%B8%BA%E5%8F%82%E6%95%B0"><span class="toc-number">4.2.4.</span> <span class="toc-text">3.2.4 dao接口方法使用一个对象作为参数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-5-dao%E6%8E%A5%E5%8F%A3%E4%B8%AD%E5%A4%9A%E4%B8%AA%E7%AE%80%E5%8D%95%E7%B1%BB%E5%9E%8B%E7%9A%84%E5%8F%82%E6%95%B0%EF%BC%8C%E4%BD%BF%E7%94%A8%E4%BD%8D%E7%BD%AE"><span class="toc-number">4.2.5.</span> <span class="toc-text">3.2.5 dao接口中多个简单类型的参数，使用位置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-6-dao%E6%8E%A5%E5%8F%A3%E5%8F%82%E6%95%B0%E6%98%AF%E4%B8%80%E4%B8%AAMap"><span class="toc-number">4.2.6.</span> <span class="toc-text">3.2.6 dao接口参数是一个Map</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-3-%E5%92%8C-%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-number">4.3.</span> <span class="toc-text">3.3 #和$的区别</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-3-1-%E5%8D%A0%E4%BD%8D%E7%AC%A6"><span class="toc-number">4.3.1.</span> <span class="toc-text">3.3.1 # 占位符</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-3-2-%E5%8D%A0%E4%BD%8D%E7%AC%A6"><span class="toc-number">4.3.2.</span> <span class="toc-text">3.3.2 $ 占位符</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-4-%E5%B0%81%E8%A3%85MyBatis%E8%BE%93%E5%87%BA%E7%BB%93%E6%9E%9C"><span class="toc-number">4.4.</span> <span class="toc-text">3.4 封装MyBatis输出结果</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-4-1-resultType"><span class="toc-number">4.4.1.</span> <span class="toc-text">3.4.1 resultType</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-4-2-resultMap"><span class="toc-number">4.4.2.</span> <span class="toc-text">3.4.2 resultMap</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-5-%E8%87%AA%E5%AE%9A%E4%B9%89%E5%88%AB%E5%90%8D"><span class="toc-number">4.5.</span> <span class="toc-text">3.5 自定义别名</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-6-%E5%88%97%E5%90%8D%E5%92%8Cjava%E5%AF%B9%E8%B1%A1%E5%B1%9E%E6%80%A7%E5%90%8D%E7%A7%B0%E4%B8%8D%E4%B8%80%E6%A0%B7%E8%A7%A3%E5%86%B3%E6%96%B9%E5%BC%8F"><span class="toc-number">4.6.</span> <span class="toc-text">3.6 列名和java对象属性名称不一样解决方式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-7-like"><span class="toc-number">4.7.</span> <span class="toc-text">3.7 like</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AC%AC%E5%9B%9B%E7%AB%A0-%E5%8A%A8%E6%80%81sql"><span class="toc-number">5.</span> <span class="toc-text">第四章 动态sql</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#4-1-if-%E6%A0%87%E7%AD%BE"><span class="toc-number">5.1.</span> <span class="toc-text">4.1  if 标签</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2-where-%E6%A0%87%E7%AD%BE"><span class="toc-number">5.2.</span> <span class="toc-text">4.2 where 标签</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-3-foreach-%E5%BE%AA%E7%8E%AF"><span class="toc-number">5.3.</span> <span class="toc-text">4.3 foreach 循环</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-4-sql%E6%A0%87%E7%AD%BE"><span class="toc-number">5.4.</span> <span class="toc-text">4.4 sql标签</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AC%AC%E4%BA%94%E7%AB%A0-MyBatis%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="toc-number">6.</span> <span class="toc-text">第五章 MyBatis配置文件</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#5-1-settings%E9%83%A8%E5%88%86"><span class="toc-number">6.1.</span> <span class="toc-text">5.1 settings部分</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-2-typeAliase-%E5%88%AB%E5%90%8D"><span class="toc-number">6.2.</span> <span class="toc-text">5.2 typeAliase 别名</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-3-%E9%85%8D%E7%BD%AE%E7%8E%AF%E5%A2%83"><span class="toc-number">6.3.</span> <span class="toc-text">5.3 配置环境</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-4-%E4%BD%BF%E7%94%A8%E6%95%B0%E6%8D%AE%E5%BA%93%E5%B1%9E%E6%80%A7%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%EF%BC%88-%EF%BC%89"><span class="toc-number">6.4.</span> <span class="toc-text">5.4 使用数据库属性配置文件（*）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-5-mapper-%E6%A0%87%E7%AD%BE"><span class="toc-number">6.5.</span> <span class="toc-text">5.5 mapper 标签(*)</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AC%AC%E5%85%AD%E7%AB%A0-PageHelper"><span class="toc-number">7.</span> <span class="toc-text">第六章 PageHelper</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/04/30/springboot%E6%B3%A8%E8%A7%A3%E7%9B%B8%E5%85%B3/" title="springboot注解相关">springboot注解相关</a><time datetime="2024-04-30T03:20:37.000Z" title="发表于 2024-04-30 11:20:37">2024-04-30</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/04/30/CommandLineRunner%E5%92%8CApplicationRunner%E7%94%A8%E6%B3%95%E4%B8%8E%E5%8C%BA%E5%88%AB/" title="CommandLineRunner和ApplicationRunner用法与区别">CommandLineRunner和ApplicationRunner用法与区别</a><time datetime="2024-04-30T02:42:21.000Z" title="发表于 2024-04-30 10:42:21">2024-04-30</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/12/08/%E5%A5%BD%E6%96%87%E6%94%B6%E8%97%8F/" title="好文收藏">好文收藏</a><time datetime="2023-12-08T08:40:13.000Z" title="发表于 2023-12-08 16:40:13">2023-12-08</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/12/08/springboot%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E4%B8%8B%E8%BD%BD/" title="springboot文件上传下载">springboot文件上传下载</a><time datetime="2023-12-08T03:30:54.000Z" title="发表于 2023-12-08 11:30:54">2023-12-08</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/12/05/linux%E4%BD%BF%E7%94%A8curl%E5%91%BD%E4%BB%A4%E5%8F%91%E9%80%81%E8%AF%B7%E6%B1%82/" title="linux使用curl命令发送请求">linux使用curl命令发送请求</a><time datetime="2023-12-05T05:56:19.000Z" title="发表于 2023-12-05 13:56:19">2023-12-05</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2021 - 2024 By 牛牛</div><div class="footer_custom_text">Hi, Welcome to my blog!</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">簡</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>